package com.study.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 跨域 Configuration Configurable写错导致跨域不生效|https://blog.csdn.net/u013066244/article/details/89061676
 */
@Configuration
@Slf4j
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        log.info("跨域请求");
        registry.addMapping("/**")
                //支持的域
                .allowedOrigins("*")
                .allowCredentials(true).allowedHeaders("Origin, X-Requested-With, Content-Type, Accept")
                .allowedMethods("GET", "HEAD", "POST", "DELETE", "PUT", "OPTIONS")
                .exposedHeaders("access-control-allow-headers", "access-control-allow-methods", "access-control-allow-origin", "access-control-max-age", "X-Frame-Options")
                .maxAge(3600)
                //允许的请求头
                .allowedHeaders("*");
    }
}
